package com.eshin.autotest;

import com.eshin.autotest.bean.OrderBean;
import com.eshin.configuration.HelloConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.util.ClassUtils;

import javax.annotation.PostConstruct;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.concurrent.Executor;

@SpringBootApplication
//@SpringBootApplication(scanBasePackages = {"com.eshin.configuration.scope"})
//@Import(HelloConfiguration.class)
/**
 * proxy-target-class属性值决定是基于接口的还是基于类的代理被创建。
 * 如果proxy-target-class 属性值被设置为true，那么基于类的代理将起作用
 * （这时需要cglib库）。如果proxy-target-class属值被设置为false或者这个
 * 属性被省略，那么标准的JDK 基于接口的代理
 *
 */
//@EnableAsync(proxyTargetClass=true,annotation = CustomAsync.class)
@EnableAsync(proxyTargetClass=true)
public class AutotestApplication {

    @PostConstruct
    public void init(){
        System.out.println("AutotestApplication init...");
    }
    public static void main(String[] args) {
        SpringApplication.run(AutotestApplication.class, args);
    }


//    @Bean
//    @ConditionalOnMissingBean(OrderBean.class)
    public OrderBean orderBean(){
        System.out.println("create custom orderbean in AutotestApplication");
        OrderBean bean = new OrderBean();
        bean.setName("eshin");
        bean.setType("1001");
        return bean;
    }

    @Bean
//    @ConditionalOnMissingBean(name="getStr20")
    public String getStrX(){
        System.out.println("helloTest in AutotestApplication");
        Set<String> names = new LinkedHashSet<>(
                SpringFactoriesLoader.loadFactoryNames(com.eshin.autotest.service.IService.class, ClassUtils.getDefaultClassLoader()));
        System.out.println(names);

        return "helloTest";
    }

    @Bean("myExecutor")
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(5);
        executor.setMaxPoolSize(10);
        executor.setQueueCapacity(25);
        executor.setThreadNamePrefix("myExecutor_");
        executor.initialize();
        return executor;
    }

}
